import {Buffer} from "buffer";
import {SetRequestNormal} from "./SetRequestNormal";
import {SetRequestNormalList} from "./SetRequestNormalList";
import {SetThenGetRequestNormalList} from "./SetThenGetRequestNormalList";
import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";


enum Choice {
    _ERR_NONE_SELECTED = -1,
    SET_REQUEST_NORMAL = 1,
    SET_REQUEST_NORMAL_LIST = 2,
    SET_THEN_GET_REQUEST_NORMAL_LIST = 3,
}


export class Set_Request implements AxdrType{

    dataCode : Buffer | null = null;
    choice : Choice = Choice._ERR_NONE_SELECTED;
    setRequestNormal : SetRequestNormal | null = null;
    setRequestNormalList : SetRequestNormalList | null = null;
    setThenGetRequestNormalList : SetThenGetRequestNormalList | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    setSetRequestNormal(setRequestNormal : SetRequestNormal){
        this.resetChoices();
        this.choice = Choice.SET_REQUEST_NORMAL;
        this.setRequestNormal = setRequestNormal;
    }

    setSetRequestNormalList(setRequestNormalList : SetRequestNormalList){
        this.resetChoices();
        this.choice = Choice.SET_REQUEST_NORMAL_LIST;
        this.setRequestNormalList = setRequestNormalList;
    }

    setSetThenGetRequestNormalList(setThenGetRequestNormalList : SetThenGetRequestNormalList){
        this.resetChoices();
        this.choice = Choice.SET_THEN_GET_REQUEST_NORMAL_LIST;
        this.setThenGetRequestNormalList = setThenGetRequestNormalList;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.SET_REQUEST_NORMAL) {
            this.setRequestNormal = new SetRequestNormal();
            codeLength += this.setRequestNormal.decode(input);
            return codeLength;
        }

        if (this.choice == Choice.SET_REQUEST_NORMAL_LIST) {
            this.setRequestNormalList = new SetRequestNormalList();
            codeLength += this.setRequestNormalList.decode(input);
            return codeLength;
        }

        if (this.choice == Choice.SET_THEN_GET_REQUEST_NORMAL_LIST) {
            this.setThenGetRequestNormalList = new SetThenGetRequestNormalList();
            codeLength += this.setThenGetRequestNormalList.decode(input);
            return codeLength;
        }

        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;

        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.SET_THEN_GET_REQUEST_NORMAL_LIST) {
            if(this.setThenGetRequestNormalList != null){
                codeLength += this.setThenGetRequestNormalList.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(3);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("setThenGetRequestNormalList is null");
            }
        }else if (this.choice == Choice.SET_REQUEST_NORMAL_LIST) {
            if(this.setRequestNormalList != null){
                codeLength += this.setRequestNormalList.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(2);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("setRequestNormalList is null");
            }
        }else if (this.choice == Choice.SET_REQUEST_NORMAL) {
            if(this.setRequestNormal != null){
                codeLength += this.setRequestNormal.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(1)
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("setRequestNormal is null");
            }
        }
        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.setRequestNormal = null;
        this.setRequestNormalList = null;
        this.setThenGetRequestNormalList = null
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    toString(): string {
        if (this.choice == Choice.SET_REQUEST_NORMAL) {
            if (this.setRequestNormal != null) {
                return "choice: {setRequestNormal: " + this.setRequestNormal + "}";
            } else {
                return "choice is setRequestNormal but setRequestNormal is null";
            }
        }else if(this.choice == Choice.SET_REQUEST_NORMAL_LIST){
            if(this.setRequestNormalList != null){
                return "choice: {setRequestNormalList: " + this.setRequestNormalList + "}";
            }else{
                return "choice is setRequestNormalList but setRequestNormalList is null";
            }
        }else if(this.choice == Choice.SET_THEN_GET_REQUEST_NORMAL_LIST){
            if(this.setThenGetRequestNormalList != null){
                return "choice: {setThenGetRequestNormalList: " + this.setThenGetRequestNormalList + "}";
            }else{
                return "choice is setThenGetRequestNormalList but setThenGetRequestNormalList is null";
            }
        } else {
            return "unknown";
        }
    }

}
